_Pragma("once");

#include "SingletonTemplate.h"
#include "ICurrentVoltageDetector.h"
#include "MultiPortDeviceManager.h"
#include "ConfigManager.h"

/**
 * 电流电压管理类
 * @author lizb
 */

class CurrentVoltageManager: public MultiPortDeviceManager<ICurrentVoltageDetector> {
    friend class SingletonTemplate<CurrentVoltageManager>;
public:

    /**
     * 获取全部格式化的电流电压信息
     * 获取之前要从设备内更新

    public String getFormattedAllCurrentAndVoltageString() {
        StringBuilder stringBuilder = new StringBuilder();
        for (IVoltageAndCurrentDetection voltageAndCurrent : voltageAndCurrentDetectionList) {
            for (String vacString : voltageAndCurrent.getFormattedAllCurrentAndVoltage()) {
                stringBuilder.append(vacString);
            }
        }
        return stringBuilder.toString();
    }
    */


    /**
     * 获取所有相位
     */
    std::string& getPhase(std::string& output) {
        ICurrentVoltageDetector* pVoltageCurrentDetector;
        ExtendDevicePtr extendDevicePtr;
        for (ExtendDeviceWeakPtr& loopDeviceWeakPtr : *mpControllers) {
            extendDevicePtr = loopDeviceWeakPtr.lock();
            if (!extendDevicePtr) {
                continue;
            }

            pVoltageCurrentDetector = static_cast<ICurrentVoltageDetector*> (&*extendDevicePtr);
            output.append(pVoltageCurrentDetector->getPhase());
        }

        return output;
    }


    /**
     * 设置所有相位
     */
    void setPhase(const std::string& phaseString) {
        ICurrentVoltageDetector* pVoltageCurrentDetector;
        ExtendDevicePtr extendDevicePtr;
        for (ExtendDeviceWeakPtr& loopDeviceWeakPtr : *mpControllers) {
            extendDevicePtr = loopDeviceWeakPtr.lock();
            if (!extendDevicePtr) {
                continue;
            }

            pVoltageCurrentDetector = static_cast<ICurrentVoltageDetector*> (&*extendDevicePtr);
            pVoltageCurrentDetector->setPhase(phaseString.substr(pVoltageCurrentDetector->mstartIndex, pVoltageCurrentDetector->mportCount));
        }

        ConfigManager& configManager = ConfigManagerSingleton::instance();
        configManager.getExtendDeviceConfig().elecInfo.phaseString = phaseString;

        configManager.saveExtendDeviceConfig();
    }


    /**
     * 更新所有数据
     */
    void updateInformation() {
        if (mforbidUpdate){
            return;
        }

        ICurrentVoltageDetector* pVoltageCurrentDetector;
        ExtendDevicePtr extendDevicePtr;
        for (ExtendDeviceWeakPtr& loopDeviceWeakPtr : *mpControllers) {
            extendDevicePtr = loopDeviceWeakPtr.lock();
            if (!extendDevicePtr) {
                continue;
            }

            pVoltageCurrentDetector = static_cast<ICurrentVoltageDetector*> (&*extendDevicePtr);
            pVoltageCurrentDetector->updateInformation();
        }
    }


    bool volatile mforbidUpdate = false;

private:
    CurrentVoltageManager() {
        initialize(ExtendDeviceEnum::ELECT);
    }

    CurrentVoltageManager(CurrentVoltageManager& ) = delete;

};


using CurrentVoltageManagerSingleton = SingletonTemplate<CurrentVoltageManager>;





